import builtins
from typing import Any, Optional, Type, Union, _GenericAlias

NoneType = type(None)


def is_inbuilt_type(type_: Type) -> bool:
    """
    判断是否是内置类型(继承的为False)
    :param type_:
    :return:
    """
    return type_.__module__ == builtins.__name__


def is_collection_type(type_: Type) -> bool:
    """
    判断是否是集合类型
    :param type_:
    :return:
    """
    return issubclass(type_, (list, set, tuple))


def is_inbuilt_single_type(type_: Type) -> bool:
    """
    判断是否是单个类型
    :param type_:
    :return:
    """
    return type_ in (str, int, float, bool)


def is_typing_optional_or_any(type_: Type):
    """
    判断类型是否是 typing.Optional 或 typing.Any
    """
    if type_ is Any:
        return True
    if isinstance(type_, _GenericAlias):
        origin_type = type_.__origin__
        if origin_type is Union:
            # 如果是 Option[*] or Union[*, None]
            if NoneType in type_.__args__:
                return True
        elif origin_type is Optional:
            return True
    return False
